//
//  TopNavView.swift
//  u17Comic
//
//  Created by hujian on 2019/7/10.
//  Copyright © 2019 hujian. All rights reserved.
//

import UIKit

let FirBtnY:CGFloat = 38
let SecBtnY:CGFloat = 75

class TopNavView: UIView {
    open var keyword: String? {
        willSet{
            if let str = newValue {
                label.text = "           " + str
            } else {
                label.text = "           搜索"
            }
        }
    }
    private var searchImg: UIImageView!
    private var label: UILabel!
    private var btnArr:[UIButton]!
    
    var offY: CGFloat = 50 {
        willSet{
            if 0 <= newValue && newValue <= 180{
                self.searchView.frame.size.width = ScreenWidth - 50 - 1.6*newValue
                if self.searchView.frame.size.width < 100{
                    self.searchView.frame.size.width = 100
                }
                if self.searchView.frame.size.width > ScreenWidth - 50{
                    self.searchView.frame.size.width = ScreenWidth - 50
                }
                
                // 自身高度
                let SHeight:CGFloat = 180 - 105/180.0 * newValue
                self.frame.size.height = SHeight
                
                // 透明度
                var alpha:CGFloat = (newValue + 20)*0.006 - 0.2
                if alpha > 1 { alpha = 1}
                if alpha < 0 {alpha = 0}
                self.backgroundColor = UIColor.init(white: 1, alpha: alpha)
//                self.alpha = alpha
                
                // loginbtn icon
                if newValue > 40 {
                    self.loginBtn.setImage(UIImage(named: "boutique_navi_packup_message"), for: .normal)
                    self.searchImg.image = UIImage(named: "boutique_navi_packup_search")
                    self.label.textColor = UIColor.gray
                    for obj in self.btnArr{
                        obj.isSelected = true
                    }
                } else {
                    self.loginBtn.setImage(UIImage(named: "boutique_navi_expend_message"), for: .normal)
                    self.searchImg.image = UIImage(named: "boutique_navi_expend_search")
                    self.label.textColor = UIColor.white
                    for obj in self.btnArr{
                        obj.isSelected = false
                    }
                }
                
                // btns
                let oriX_MAX:CGFloat = 160
                let btnWidth_min:CGFloat = (ScreenWidth - oriX_MAX)*1.0/4.0
                let btnWidth_max:CGFloat = (ScreenWidth)*1.0/4.0
                
                let Y1 = SecBtnY - (SecBtnY-FirBtnY)*1.0*newValue/180.0
                let W = btnWidth_max - (btnWidth_max - btnWidth_min)*1.0*newValue/180.0
                let centerY_MIN:CGFloat = FirBtnY + 30/2
                let centerY_MAX:CGFloat = SecBtnY + 38/2
                for (idx, obj) in self.btnArr.enumerated(){
                    let X1_MIN:CGFloat = CGFloat(idx)*btnWidth_max
                    let X1_MAX:CGFloat = CGFloat(idx)*btnWidth_min + oriX_MAX
                    let X1:CGFloat = X1_MIN + (X1_MAX - X1_MIN)*1.0*newValue/180.0
                    obj.frame.origin.x = X1
                    obj.frame.origin.y = Y1
                    obj.frame.size.width = W
                    obj.center.y = centerY_MAX - (centerY_MAX - centerY_MIN)*1.0*newValue/180
                }
            }
        }
    }
    
    open lazy var loginBtn:UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setImage(UIImage(named: "boutique_navi_expend_message"), for: .normal)
        btn.frame = CGRect(x: 2, y: FirBtnY, width: 26, height: 26)
        return btn
    }()
    
    open lazy var searchView: UIView = {
        let view = UIView(frame: CGRect(x: 35, y: 38, width: ScreenWidth - 50, height: 30))
        view.backgroundColor = UIColor.clear
        let label = UILabel(frame: view.bounds)
        label.layer.cornerRadius = 15;
        label.layer.masksToBounds = true
        label.backgroundColor = UIColor(r: 230, g: 230, b: 230, a: 0.5)
        label.text = "           搜索"
        label.textColor = UIColor.white
        label.font = UIFont.systemFont(ofSize: 15)
        view.addSubview(label)
        label.snp.makeConstraints({ (make) in
            make.left.equalTo(view)
            make.right.equalTo(view)
            make.centerY.equalTo(view)
            make.height.equalTo(30)
        })
        self.label = label
        
        let imgView: UIImageView = UIImageView(image: UIImage(named: "boutique_navi_expend_search"))
        imgView.contentMode = UIView.ContentMode.scaleAspectFit
        view.addSubview(imgView)
        imgView.snp.makeConstraints({ (make) in
            make.left.equalTo(view).offset(15)
            make.centerY.equalTo(view)
            make.width.height.equalTo(18)
        })
        self.searchImg = imgView
        
        return view
    }()
    
    private var btnRank:UIButton!
    private var btnVIP:UIButton!
    private var btnSubscribe:UIButton!
    private var btnCategory:UIButton!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.clear
        addSubview(loginBtn)
        addSubview(searchView)
        
        let btn1 = makeButton(title: "排行", imgName: "boutique_fast_expend_rank",img2: "boutique_fast_packup_rank")
        btnRank = btn1
        let btn2 = makeButton(title: "VIP", imgName: "boutique_fast_expend_vip",img2: "boutique_fast_packup_vip")
        btnVIP = btn2
        let btn3 = makeButton(title: "订阅", imgName: "boutique_fast_expend_subscrip",img2: "boutique_fast_packup_subscrip")
        btnSubscribe = btn3
        let btn4 = makeButton(title: "分类", imgName: "boutique_fast_expend_classificat",img2: "boutique_fast_packup_classificat")
        btnCategory = btn4
        let arr = [btn1,btn2,btn3,btn4]
        let btnW:CGFloat = ScreenWidth/4.0
        for (idx, obj) in arr.enumerated(){
            let X:CGFloat = CGFloat(idx)*btnW
            obj.frame = CGRect(x: X, y: SecBtnY, width: btnW, height: 38)
            addSubview(obj)
        }
        self.btnArr = arr
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func makeButton(title:String, imgName:String, img2:String) -> UIButton {
        let button = UIButton(type: .custom)
        button.setTitle(title, for: .normal)
        button.setTitle("", for: .selected)
        button.setImage(UIImage(named: imgName), for: .normal)
        button.setImage(UIImage(named: img2), for: .selected)
        button.setTitleColor(UIColor.white, for: .normal)
        button.setTitleColor(UIColor.theme, for: .selected)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        return button
    }
}
